package com.h819.core.web;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class InitLog4jServlet extends HttpServlet {

	private Logger log = Logger.getLogger(InitLog4jServlet.class);
	
	  //Initialize global variables
	  public void init() throws ServletException {
	    try {
	      String prefix = getServletContext().getRealPath("/");
	      String file = getInitParameter("log4j-init-file");
	      // if the log4j-init-file is not set, then no point in trying
	      if (file != null) {
	        PropertyConfigurator.configure(prefix + file);
	        log.info("---Init log4j.properties succeed ---");
	       
	      }
	    }
	    catch (Exception e) {
	    	log.info("---Init log4j.properties failed ---");
	    	e.printStackTrace();
	    }
	  }

	  //Process the HTTP Get request
	  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
	      ServletException, IOException {
	  }

	  //Process the HTTP Post request
	  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
	      ServletException, IOException {
	    doGet(request, response);
	  }

	  //Clean up resources
	  public void destroy() {
	  }

}

